﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Windows.Threading;

namespace HomeWP
{
    public partial class VariableControl : UserControl
    {
        private HMDataContext hMDB = ((App)App.Current).HMData.DataContext; 
        public VariableControl()
        {
            InitializeComponent();

            dt.Interval = new TimeSpan(0, 0, 10);
            dt.Tick +=new EventHandler(dt_Tick);
            dt.Start();

            if (hMDB.HMCCUSettings.Count(t => t.ConfigID == "TEST") > 0)
            {
                CCUSet = hMDB.HMCCUSettings.Single(t => t.ConfigID == "TEST");
            }

        }
        private CCUSettings CCUSet;
        public string CCUIP { get; set; }
        public int CCUPort { get; set; }
        public SystemVariable HMVar { get; set; }

        DispatcherTimer dt = new DispatcherTimer();



        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            HMVar = this.DataContext as SystemVariable;
            _varName.Text = HMVar.Name;
            _varValue.Text = HMVar.Value + HMVar.Unit;
            _varUnit.Text = HMVar.Timestamp.ToString();

       /* Vaiable dont wörk     if (((App)App.Current).HMData.IsFavorit(Address))
            {
                mi_Favorit.Visibility = System.Windows.Visibility.Collapsed;
                mi_DelFavorit.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                mi_Favorit.Visibility = System.Windows.Visibility.Visible;
                mi_DelFavorit.Visibility = System.Windows.Visibility.Collapsed;
            }
        * 
        * */
        }

        void dt_Tick(object sender, EventArgs e)
        {
            dt.Stop();
            if(HMVar.Timestamp != hMDB.HMVariables_.Single(t=>t.ID == HMVar.ID).Timestamp)
            {
                HMVar = hMDB.HMVariables_.Single(t => t.ID == HMVar.ID);
                _varName.Text = HMVar.Name;
                _varValue.Text = HMVar.Value + HMVar.Unit;
                _varUnit.Text = HMVar.Timestamp.ToString();
            }

            dt.Start();
        }

        private void mi_DelFavorit_Click(object sender, RoutedEventArgs e)
        {
        //    ((App)App.Current).HMData.DeleteFavorit(Address);
        }

        private void mi_Favorit_Click(object sender, RoutedEventArgs e)
        {
         //   ((App)App.Current).HMData.AddFavorit(Address);
        }


    }
}
